﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xaml;
using LIK.WakeOnLan.Network;

namespace LIK.WakeOnLan
{
    internal static class ApplicationConfigSaver
    {
        private static readonly string AppFolder;

        static ApplicationConfigSaver()
        {
            AppFolder = Path.GetDirectoryName(Application.ExecutablePath);
        }

        internal static void SaveQuickList(Dictionary<string, NetworkClient> nClients)
        {
            string favConfigFile = Path.Combine(AppFolder, "fav.config");

            try
            {
                using (StreamWriter writer = File.CreateText(favConfigFile))
                {
                    XamlServices.Save(writer, nClients);
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        internal static Dictionary<string, NetworkClient> LoadQuickList()
        {
            string favConfigFile = Path.Combine(AppFolder, "fav.config");
            if (!File.Exists(favConfigFile))
            {
                return new Dictionary<string, NetworkClient>();
            }
            Dictionary<string, NetworkClient>  nClients;
            try
            {
                using (StreamReader tr = new StreamReader(favConfigFile))
                {
                    nClients = (Dictionary<string, NetworkClient>) XamlServices.Load(tr);
                }
            }
            catch //(Exception ex)
            {
                nClients = new Dictionary<string, NetworkClient>();
            }
            return nClients;
        }

        internal static void SaveNetworkClients(NetworkClients nClients)
        {
            string favConfigFile = Path.Combine(AppFolder, "clients.config");
            try
            {
                using (StreamWriter writer = File.CreateText(favConfigFile))
                {
                    XamlServices.Save(writer, nClients);
                }
            }
            catch (Exception ex)
            {

            }
        }

        internal static NetworkClients LoadNetworkClients()
        {
            string favConfigFile = Path.Combine(AppFolder, "clients.config");
            NetworkClients nClients;
            try
            {
                using (StreamReader tr = new StreamReader(favConfigFile))
                {
                    nClients = (NetworkClients)XamlServices.Load(tr);
                }
            }
            catch (Exception ex)
            {
                nClients = new NetworkClients();
            }
            return nClients;
        }


    }
}
